package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ：lintao
 * @date ：Created in 2020/12/3
 * @description ：会员服务接口实现类
 * @version: 1.0
 */
@Service
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        if (member.getPassword() != null) {
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }else {
            member.setPassword(MD5Utils.md5("123456"));
        }
        memberDao.add(member);
    }

    // 根据月份统计会员数量
    @Override
    public List<Integer> finMemberCountByMonth(List<String> month) {
        List<Integer> list = new ArrayList<>();
        for (String m : month) {
            m += ".31"; // 格式：2020.12.31
            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> findMemberCountBySex() {
        /*List<Map<String, Object>> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        Integer manCount = memberDao.findCountBySex("1");
        map.put("name","男");
        map.put("value",manCount);
        list.add(map);
        Map<String,Object> map1 = new HashMap<>();
        Integer womanCount = memberDao.findCountBySex("2");
        map1.put("name","女");
        map1.put("value",womanCount);
        list.add(map1);*/
        return memberDao.findCountBySex();
    }

    @Override
    public List<Map<String, Object>> findMemberCountByAge() {
        List<Map<String, Object>> list = new ArrayList<>();

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR,-18);  // 18
        // 0 - 18  >=oldDate
        Map<String,Object> map1 = new HashMap<>();
        String oldDate =new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());  // 18
        Integer count = memberDao.findCountByAfterBirthday(oldDate);
        map1.put("name","0-18");
        map1.put("value",count);
        list.add(map1);
        // 18 - 30
        calendar.add(Calendar.YEAR,-12); // 30
        String oldDate2 =new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()); // 30

        count = memberDao.finCountByBetweenBirthday(oldDate2,oldDate);
        Map<String,Object> map2 = new HashMap<>();
        map2.put("name","18-30");
        map2.put("value",count);
        list.add(map2);
        // 30 - 45
        calendar.add(Calendar.YEAR,-15); // 45
        oldDate =new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()); // 45
        count = memberDao.finCountByBetweenBirthday(oldDate,oldDate2);
        Map<String,Object> map3 = new HashMap<>();
        map3.put("name","30-45");
        map3.put("value",count);
        list.add(map3);
        // 45以上
        count = memberDao.findCountByBeforeBirthday(oldDate);
        Map<String,Object> map4 = new HashMap<>();
        map4.put("name","45以上");
        map4.put("value",count);
        list.add(map4);
        return list;
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Member> page = memberDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id删除会员
     * @param id
     */
    @Override
    public void delete(Integer id) {
        // 查询该会员是否有预约未到诊信息
        Order order = orderDao.findByMemberId(id);
        if (order != null && "未到诊".equals(order.getOrderStatus())){
            throw new RuntimeException("该会员有预约未到诊记录");
        }
        memberDao.deleteById(id);
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public void edit(Member member) {
        memberDao.edit(member);
    }


}
